const app = getApp()
Page({
  data: {
    loading: true,
    focus:false,
    page: 0,
    cate_id: 0,
    keyword:'',
    product: [],
  },
  //监听页面初次载入完成
  onLoad: function (event) {
    var that = this;
    if (!app.util().isNull(event.focus)) {
      that.setData({
        focus:true
      })
    }
    if (!app.util().isNull(event.id)) {
      that.setData({
        cate_id: event.id
      })
    }
    that.getApi();
  },
  //上拉加载
  onReachBottom: function () {
    var that = this;
    that.setData({
      loading: true,
    });
    that.getApi();
  },
  //获取API
  getApi: function () {
    let that = this;
    if (that.data.loading) {
      var parms = {
        cate_id: that.data.cate_id,
        keyword: that.data.keyword,
        page: that.data.page + 1
      }
      app.api().Get('api/v1/bestbao/product/index', parms, function (result) {
        if (result.code == 200) {
          var product = that.data.product;
          for (let i in result.data) {
            product.push(result.data[i]);
          }
          that.setData({
            product: product,
            page: parms.page,
          });
        }
      })
      that.setData({
        loading: false,
      });
    }
  },
  //搜索
  onSearch: function (event) {
    if (app.util().isNull(event.detail)) {
      wx.showModal({
        content: '请输入产品关键词', showCancel: false
      })
    } else {
      this.setData({
        loading: true,
        page: 0,
        product: [],
        keyword: event.detail
      })
      this.getApi();
    }
  },
  //取消搜索
  onCancel() {
    this.setData({
      loading: true,
      keyword: '',
      page: 0,
      product: [],
    })
    this.getApi()
  }
})